#include<iostream>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<cstdlib>
#include<cstring>
#include<pthread.h>
#include<unistd.h>
#include<arpa/inet.h>

using namespace std;

void *Routinue(void *arg)
{
    pthread_detach(pthread_self());
    int fd = *(int*)arg;
    delete (int*)arg;

    //close(fd);
    while(1){
      std::cout << "thread is running: "<<pthread_self()<<fd<< std::endl;
        sleep(1);
    }

    return nullptr;
}


int main()
{
  int lsock=socket(AF_INET,SOCK_STREAM,0);
  if(lsock<0)
  {
    cout<<"creat sock fail"<<endl;
    return 1;
  }
  int opt=1;
  setsockopt(lsock,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt));
  struct sockaddr_in local;
  memset(&local,0,sizeof(local));
  local.sin_family=AF_INET;
  local.sin_port=htons(8081);
  local.sin_addr.s_addr=INADDR_ANY;


  if(bind(lsock,(struct sockaddr*)&local,sizeof(local))<0)
  {
    cout<<"bind fail"<<endl;
    return 2;
  }

  if(listen(lsock,5)<0)
  {
    cout<< "listen fail"<<endl;
  }

  struct sockaddr_in peer;
  for(;;)
  {
//  socklen_t len=sizeof(peer);
  //  int sock = accept(lsock,(struct sockaddr*)&peer,&len);
  //  if(sock<0)
  //  {
  //    cout<<"accpet error"<<endl;
  //  }
  //  cout<<"get a new sock "<<sock<<endl;
  //  int *p = new int(sock);
  //  pthread_t tid;
  //  pthread_create(&tid,nullptr,Routinue,p);
  }
  return 0;
}
